{include file="public/layout" /}
<script type="text/javascript" src="__PUBLIC__/plugins/laydate/laydate.js"></script>
<body class="bodystyle eyou_body_xin" style="cursor: default; -moz-user-select: inherit;min-width:auto;">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
{include file="shop_product/left" /}
<div class="page arctype min-hg-c-10" style="min-width: auto; margin-left: 100px;">
    <div class="fixed-bar">
        <div class="item-title">
            <a class="back_xin" href="javascript:history.back();" title="返回"><i class="iconfont e-fanhui"></i></a>
            <div class="subject"><h3>新增商品分类</h3><h5></h5></div>
        </div>
    </div>

    <form class="form-horizontal" id="post_form" action="{:url('ShopProduct/arctype_add')}" method="post">
        <div class="ncap-form-default">
            <dl class="row">
                <dt class="tit"> <label for="typename"><em>*</em>分类名称</label> </dt>
                <dd class="opt"> <input type="text" name="typename" id="typename" class="input-txt"> <p class="notic">保持唯一性，不可重复</p> </dd>
            </dl>

            <dl class="row">
                <dt class="tit"> <label for="topid">上级分类</label> </dt>
                <dd class="opt">
                    <select class="small form-control" id="topid" name="topid" onchange="AjaxGetUnderType(this, 'topid');">
                        <option value="0" data-dirpath="" selected="true">顶级分类</option>
                        {volist name="arctype_list" id="vo"}
                        <option value="{$vo.id}" data-dirpath="{$vo.dirpath}" {eq name="$topid" value="$vo.id"} selected="true" {/eq}>{$vo.typename}</option>
                        {/volist}
                    </select>
                    <select class="small form-control" id="parent_id" name="parent_id" onchange="AjaxGetUnderType(this, 'parent_id');" style="display: none;">
                        {volist name="arctype_list2" id="vo"}
                        <option value="{$vo.id}" data-dirpath="{$vo.dirpath}" {eq name="$parent_id" value="$vo.id"} selected="true" {/eq}>{$vo.typename}</option>
                        {/volist}
                    </select>
                    <input type="hidden" id="dirpath" name="dirpath" value="{$predirpath}">
                </dd>
            </dl>

            <dl class="row">
                <dt class="tit"> <label>分类图片</label> </dt>
                <dd class="opt">
                    <div class="input-file-show div_litpic_local">
                        <span class="show">
                            <a id="litpic_a" target="_blank" class="nyroModal" rel="gal" href="javascript:void(0);" target="_blank">
                                <i id="litpic_i" class="fa fa-picture-o" onmouseover="" onmouseout="layer.close(layer_tips);"></i>
                            </a>
                        </span>
                        <span class="type-file-box">
                            <input type="text" id="litpic" name="litpic" class="type-file-text" autocomplete="off">
                            <input type="button" name="button" value="选择上传..." class="type-file-button">
                            <input class="type-file-file" onclick="GetUploadify(1, '', 'allimg', 'litpic_call_back');" size="30" hidefocus="true" nc_type="change_site_logo" title="点击前方预览图可查看大图，点击按钮选择文件并提交表单后上传生效">
                        </span>
                    </div>
                </dd>
            </dl>

            <!-- <dl class="row">
                <dt class="tit"> <label>广告图片</label> </dt>
                <dd class="opt">
                    <div class="input-file-show div_litpic_local">
                        <span class="show">
                            <a id="ad_image_a" target="_blank" class="nyroModal" rel="gal" href="javascript:void(0);" target="_blank">
                                <i id="ad_image_i" class="fa fa-picture-o" onmouseover="" onmouseout="layer.close(layer_tips);"></i>
                            </a>
                        </span>
                        <span class="type-file-box">
                            <input type="text" id="ad_image" name="ad_image" class="type-file-text" autocomplete="off">
                            <input type="button" name="button" value="选择上传..." class="type-file-button">
                            <input class="type-file-file" onclick="GetUploadify(1, '', 'allimg', 'ad_image_call_back');" size="30" hidefocus="true" nc_type="change_site_logo" title="点击前方预览图可查看大图，点击按钮选择文件并提交表单后上传生效">
                        </span>
                    </div>
                </dd>
            </dl> -->

            <!-- <dl class="row">
                <dt class="tit"> <label for="ad_link">广告链接</label> </dt>
                <dd class="opt">
                    <input type="hidden" name="ad_link_id" id="ad_link_id">
                    <input type="hidden" name="ad_host_id" id="ad_host_id">
                    <input type="text" name="ad_link" id="ad_link" class="input-txt" readonly="readonly" style="width: 300px !important;">
                    <a href="javascript:void(0);" data-ad_host_id="0" data-ad_link_id="0" onclick="selectLink(this);" class="ncap-btn ncap-btn-green">选择链接</a>
                    <a href="javascript:void(0);" onclick="clearLink();">清空</a>
                </dd>
            </dl> -->

            <dl class="row">
                <dt class="tit"> <label>隐藏分类</label> </dt>
                <dd class="opt">
                    <label class="curpoin"><input id="is_hidden1" name="is_hidden" value="1" type="radio">是</label> &nbsp; 
                    <label class="curpoin"><input id="is_hidden0" name="is_hidden" value="0" type="radio" checked="checked">否</label>
                    <p class="notic">隐藏之后，不显示在前台页面</p>
                </dd>
            </dl>
        </div>

        <div class="ncap-form-default">
            <div class="bot">
                <a href="JavaScript:void(0);" onclick="arctypeSubmit();" class="ncap-btn-big ncap-btn-green" id="submitBtn">保存</a>
            </div>
        </div> 
    </form>
</div>
<script type="text/javascript">
    $(function() {
        // 如果存在上级分类ID则执行
        var parentShow = {$parent_show};
        if (parseInt(parentShow) === 1) $('#parent_id').show();
    });

    // 查询下级分类
    function AjaxGetUnderType(obj, field) {
        if ('topid' == field) {
            // 如果上级分类ID不存在则结束
            var parentID = $(obj).val();
            if (parseInt(parentID) === 0) {
                $('#dirpath').val('');
                $('#parent_id').hide().empty().html('');
                return false;
            }
            
            // 查询下级分类
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/ajaxGetUnderType')}",
                data: {parent_id: parentID, _ajax: 1},
                dataType: 'json',
                success: function(res){
                    if (1 == res.code) {
                        if (res.data.selecthtml) {
                           $('#parent_id').show().empty().html(res.data.selecthtml);
                        } else {
                           $('#parent_id').hide().empty().html('');
                        }
                    } else {
                       $('#parent_id').hide().empty().html('');
                    }
                }
            });
        }

        // 获取分类路径
        var dirpath = $('#'+field+' option:selected').data('dirpath');
        if (undefined == dirpath && 'parent_id' == field) dirpath = $('#topid option:selected').data('dirpath');
        $('#dirpath').val(dirpath ? dirpath : '');
    }

    // 加载选择的图片信息(分类图片)
    function litpic_call_back(paths) {
        $("#litpic").val(paths);
        $("#litpic_a").attr('href', paths);
        $("#litpic_i").attr('onmouseover', "layer_tips=layer.tips('<img src="+paths+" class=\\'layer_tips_img\\'>',this,{tips: [1, '#fff']});");
    }

    // 加载选择的图片信息(广告图片)
    function ad_image_call_back(paths) {
        $("#ad_image").val(paths);
        $("#ad_image_a").attr('href', paths);
        $("#ad_image_i").attr('onmouseover', "layer_tips=layer.tips('<img src="+paths+" class=\\'layer_tips_img\\'>',this,{tips: [1, '#fff']});");   
    }

    function selectLink(obj) {
        selectAdLink(obj, function(jsondata){
            var link_id = jsondata.link_id;
            var host_id = jsondata.host_id;
            var link_names = jsondata.link_names;
            $(obj).attr('data-ad_link_id', link_id);
            $(obj).attr('data-ad_host_id', host_id);
            $('#ad_link').val(link_names);
            $('#ad_link_id').val(link_id);
            $('#ad_host_id').val(host_id);
            layer.closeAll();
        });
    }

    // 清空已选择的链接信息
    function clearLink() {
        $('#ad_link').val('');
        $('#ad_link_id').val(0);
        $('#ad_host_id').val(0);
    }

    // 新增商品分类
    function arctypeSubmit() {
        if ($('#typename').val() == '') {
            showErrorMsg('请输入分类名称');
            $('#typename').focus();
            return false;
        }
        // 执行新增
        $.ajax({
            type: 'post',
            url : "{:url('ShopProduct/arctype_add')}",
            data: $('#post_form').serialize(),
            dataType: 'json',
            success : function(res) {
                if (1 === parseInt(res.code)) {
                    layer.msg(res.msg, {icon: 1, time: 1500}, function() {
                        window.location.href = res.url;
                    });
                } else {
                    showErrorMsg(res.msg);
                }
            },
            error: function(e) {
                showErrorAlert(e.responseText);
            }
        });
    }
</script>

{include file="public/footer" /}